﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;

//Log4Net einbinden
using log4net;
using log4net.Config;

//using UsbGuard.Logger;

//[assembly: log4net.Config.XmlConfigurator(Watch = true)]

namespace UsbGuard.Alarm
{
    public partial class frmMain : Form
    {
        private bool ColorState = false;

        private int freq = 3700;
        private int leng = 500;

        //Achtung: Passwort fest im Quellcode
        private const string PWD= "test";

        

        private static readonly ILog log = LogManager.GetLogger(typeof(frmMain));

        public frmMain()
        {
            InitializeComponent();

            string appPath =
                System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
            log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(appPath + "\\log.xml"));

            log.Error("Alarm wurde ausgelöst");

            System.Threading.Thread x = new System.Threading.Thread(killall);
            x.Start();

        }




        

        private void frmMain_Load(object sender, EventArgs e)
        {
            try
            {
                //Log.logetwas("Error wurde aufgerufeen!");
                
            }
            catch (Exception ex)
            { 
            
            }

            int TaskbarWindowHandle = FindWindow("Shell_TrayWnd", "");
            ShowWindow(TaskbarWindowHandle, 1);
            
        }

        [DllImport("user32.dll")]
        private static extern int FindWindow(string className, string windowText);

        [DllImport("user32.dll")]
        private static extern int ShowWindow(int hwnd, int command);



        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == (Keys.Alt | Keys.F4) | keyData == Keys.LWin | keyData == Keys.LControlKey | keyData == Keys.Escape)
            {
                // z.B. eine MessageBox ausgeben und als behandelt "true" zurückliefern
                MessageBox.Show("Versuchs nicht mal.");

                
                return true;
            }
            // sonst Standardverhalten
            return base.ProcessDialogKey(keyData);

        }
        
        public bool killing = true;
        public void killall()
        {
            while (killing)
            {
                System.Diagnostics.Process.GetProcesses().Where(x => x.ProcessName == "taskmgr").ToList().ForEach(x => TerminateProcess(x.Handle,1));
                System.Diagnostics.Process.GetProcesses().Where(x => x.ProcessName == "explorer").ToList().ForEach(x => TerminateProcess(x.Handle, 1));
                System.Threading.Thread.Sleep(500);
            }
        }



        private void tmrColor_Tick(object sender, EventArgs e)
        {
            if (ColorState)
            {
                this.BackColor = Color.Red;
                ColorState = false;
            }
            else
            {
                this.BackColor = Color.GreenYellow;
                ColorState = true;
            }

            try
            {
                Console.Beep(freq, leng);
                freq = freq + 100;
                leng = leng - 15;
            }//TODO: Catch bearbeiten
            catch
            {
            }
            
        }


        private void tmrBeep_Tick(object sender, EventArgs e)
        {
            Console.Beep(3700, 1000);
        }




        private void maskedTextBox1_TextChanged(object sender, EventArgs e)
        {
            if (maskedTextBox1.Text == PWD)
            {
                killing = false;
                System.Threading.Thread.Sleep(501);
                //System.Diagnostics.Process.Start("explorer");
                ShellExecute(0, "start", "explorer", "", "", 0);
                System.Diagnostics.Process.Start("C:\\Windows\\explorer.exe");
                this.Dispose();
            }
        }



        [DllImport("shell32.dll", EntryPoint = "ShellExecute")]
        public static extern long ShellExecute(int hwnd, string cmd, string file, string param1, string param2, int swmode);


        [DllImport("kernel32.dll", EntryPoint = "TerminateProcess")]
        public static extern int TerminateProcess(IntPtr hProcess, uint uExitCode);

    }
}
